///////////////////////////////////////////////////////////////////
// Student name:Jon Hersack and Caleb Reinking
// Course: COSC 3403
// Assignment: #7 - Initial Software Construction
// File name:Deck.java
// Purpose: Provides singleton access to deck, which allows classes
//          to get cards
// Assumptions:none known
// Limitations:Deck does not shuffle cards well at this point. A good algorithm
//             needs to be worked out for this action.
// Development Computer:Intel core2 duo 2.67Ghz
// Operating System: windows
// Integrated Development Environment (IDE): netbeans 6.5
// Compiler: Javac: java 1.6.0_13
///////////////////////////////////////////////////////////////////


import java.util.*;
import java.io.*;
//singleton design based on
//http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
public class Deck
{
    private static Deck instance = null;

    int[] initial_Cards = null;
    Scanner deckFile = null;

    int numCardsLeft;
    Stack<Card> deckStack = null;

   //###########################################
   private Deck() {

       try{//reads in standard MilleBornes deck from file
            deckFile = new Scanner(new File("MB_default_deck.deck"));
       } catch( FileNotFoundException fnfe )
       {
           System.out.println(fnfe.getMessage());
       }


       int numTypes = deckFile.nextInt();
       int totalCards = deckFile.nextInt();
       deckStack = new Stack<Card>();

       initial_Cards = new int[totalCards];

       int cardType;
       int numOfCardType;
       int currentDeckIndex = 0;
       while(deckFile.hasNext())
       {
           cardType = deckFile.nextInt();
           numOfCardType = deckFile.nextInt();

           for( int i = 0 ; i < numOfCardType ; i++ )
           {
               initial_Cards[currentDeckIndex] = cardType;
               currentDeckIndex++;
           }
       }

       deckFile.close();

       //resetDeck();
       shuffleDeck();
      
   }//end constructor
   
   public static Deck getInstanceOf() {
      if(instance == null) {
         instance = new Deck();
      }
      return instance;
   }

    //#########################################
    void resetDeck()
    {
        deckStack.clear();
        for(int i=0; i < initial_Cards.length; i++)
        {
            deckStack.push(new Card(initial_Cards[i]));
        }//end for

    }//end resetDeck

    //#########################################
    Boolean isEmpty()
    {
       return deckStack.isEmpty();
    }//end isEmpty

    //#########################################
    Card drawCard()
    {
            return deckStack.pop();       
    }//end drawCard

    //#########################################
    // This shuffle algorithm needs some major work. Unfortunately, a player
    // right now ends up with many similar cards in his hand
    void shuffleDeck()
    {
        deckStack.clear();
        Random randGen = new Random();
        int randomIndex = 0;
        Boolean[] usedFlags = new Boolean[initial_Cards.length];

        for( int i = 0 ; i < initial_Cards.length ; i++ )
            usedFlags[i] = false;

        for( int i = 0 ; i < initial_Cards.length ; i++ )
        {
            randomIndex = randGen.nextInt(initial_Cards.length);
            while( usedFlags[randomIndex] == true )
               randomIndex = (randomIndex+3)%initial_Cards.length;

            deckStack.push(new Card(initial_Cards[randomIndex]));
            usedFlags[randomIndex] = true;

        } // end for
    
    }//end shuffleDeck

    //###############################
    public void printDeck()
    {
        for( int i = 0 ; i < initial_Cards.length ; i++ )
        {
            System.out.println( initial_Cards[i] );
        } // end for
    } // end printDeck

    //#################################
    public static void main(String [ ] args)
    {
        Deck myDeck = Deck.getInstanceOf();
        
    }// end test main

}//end class Deck


